package com.video.service.impl;

import com.api.client.IdClient;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.dto.DraftVideoItemAddVideoUrlDTO;
import com.common.domain.po.DraftVideoItem;
import com.common.utils.AliOSSUtils;
import com.video.mapper.DraftVideoItemMapper;
import com.video.service.DraftVideoItemService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 路亚
 * @version 0.1
 * @className DraftVideoItemServiceImpl
 * @description
 * @date 2024/12/11 19:08
 * @since jdk11
 */
@Service
@RequiredArgsConstructor
public class DraftVideoItemServiceImpl extends ServiceImpl<DraftVideoItemMapper, DraftVideoItem> implements DraftVideoItemService {

    private final DraftVideoItemMapper draftVideoItemMapper;

    private final AliOSSUtils aliOSSUtils;

    private final IdClient idClient;

    @Override
//    @GlobalTransactional
    @Transactional
    public void addVideoUrl(List<DraftVideoItemAddVideoUrlDTO> dtoList) {
        // 批量修改会循环操作数据库，导致性能下降，所以先删除所有数据，再添加
        // 1.先查出所有要添加url的id
        // 获取所有的集数
        List<Integer> numList = dtoList.stream().mapToInt(DraftVideoItemAddVideoUrlDTO::getNum).boxed().collect(Collectors.toList());
        // 获取短剧id
        Long videoId = dtoList.get(0).getVideoId();
        List<DraftVideoItem> draftVideoItemList = draftVideoItemMapper.queryByNum(videoId, numList);
        // 2.删除所有查出的数据
        List<Long> itemIds = draftVideoItemList.stream().mapToLong(DraftVideoItem::getId).boxed().collect(Collectors.toList());
        removeByIds(itemIds);
        // 3.添加数据
        for (DraftVideoItem draftVideoItem : draftVideoItemList) {
            // 查找dtoList中num匹配的DTO对象
            DraftVideoItemAddVideoUrlDTO dto = dtoList.stream()
                    .filter(dt -> dt.getNum().equals(draftVideoItem.getNum()))
                    .findFirst()
                    .orElse(null); // 如果没有找到匹配的DTO对象，则为null
            // 如果找到了匹配的DTO对象，则将videoUrl赋值给draftVideoItem
            if (dto != null) {
                // 获取原始URL
                String originalUrl = dto.getVideoUrl();
                int lastIndexOfSlash = originalUrl.lastIndexOf('/');
                String prefix = originalUrl.substring(0, lastIndexOfSlash);
                // 提取文件名
                String fileName = originalUrl.substring(lastIndexOfSlash + 1);
                String newFilePath = "dj/video/" + videoId + "/" + draftVideoItem.getId() + "/" + draftVideoItem.getId() + ".mp4";
                // 构造新的URL，包含前缀和新的文件路径
                String newFileName = prefix + "/" + newFilePath;
                // 假设aliOSSUtils.moveFile方法接受新旧文件的相对路径
                try {
                    aliOSSUtils.moveFile(fileName, newFilePath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                // 更新draftVideoItem对象的videoUrl属性
                draftVideoItem.setVideoUrl(newFileName);
            }
        }
        saveBatch(draftVideoItemList);
    }
}
